package acwing.图;

import java.util.*;

/**
 * spfa
 *
 * @author yang
 * @date 2022/1/8
 */
public class spfa {

    static int N = 100010;
    static int w[] = new int[N];
    static int h[] = new int[N];
    static int e[] = new int[N];
    static int ne[] = new int[N];
    static int idx, n, m, INF = 0x3f3f3f3f;
    static boolean[] st = new boolean[N];
    static int[] dist = new int[N];

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        Arrays.fill(h, -1);
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            add(x,y,z);
        }
        int t = spfa();
        if (t != 0) {
            System.out.println(t);
        } else {
            System.out.println("impossible");
        }
    }

    private static int spfa() {

        Arrays.fill(dist, INF);
        Queue<Integer> q = new LinkedList<>();
        dist[1] = 0;
        q.add(1);
        st[1] = true;

        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;

            for (int i = h[t]; i != -1; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (!st[j]) {
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
        if (dist[n] == INF) return 0;
        return dist[n];

    }

    private static void add(int x, int y, int z) {
        e[idx] = y;
        w[idx] = z;
        ne[idx] = h[x];
        h[x] = idx++;
    }
}
